<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
/**
 * Basketball extension for Joomleague
 * @version $Id: show_player.tpl.php 98 2008-02-25 23:38:14Z julien.vonthron $
 * @package JlgBasketballExtension
 * @copyright Copyright (C) 2007 Julien Vonthron. All rights reserved.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 * JlgBasketballExtension is free software and parts of it may contain or be derived from the
 * GNU General Public License or other free or open source software licenses.
 * 
 * contact: julien.vonthron[at]gmail.com 
 *   
 * Joomleague is a League Management and Prediction Game for Joomla!
 * Homepage: http://www.joomleague.de 
 *  
 */

/**
 * returns true if the event stat should be displayed
 *
 * @param int $eventId
 * @return boolean  
 */   
function displayEvent($eventId)
{
    global $overall_config;
    $averages_event_set = array ( 
        $overall_config['event_3pt_made'],
        $overall_config['event_3pt_attempts'],
        $overall_config['event_fg_made'],
        $overall_config['event_fg_attempts'],
        $overall_config['event_ft_made'],
        $overall_config['event_ft_attempts'],
        $overall_config['event_ppg'],
        $overall_config['event_rpg'],
        $overall_config['event_apg'],
        $overall_config['event_spg'],
        $overall_config['event_bpg'],
        $overall_config['event_3pt_per'],
        $overall_config['event_fg_per'],
        $overall_config['event_ft_per']
                    );
    return in_array( $eventId, $averages_event_set );
}

/**
 * print formatted event stat
 *
 * @param int $eventId
 * @param int $stat value of the event stat
 */ 
function printEventStat($eventId, $stat)
{
    global $overall_config;
    switch ($eventId)
    {
        case $overall_config['event_ppg']:
        case $overall_config['event_rpg']:
        case $overall_config['event_apg']:
          printf( '%.1f', $stat );
          break;
        case $overall_config['event_spg']:
        case $overall_config['event_bpg']:
          printf( '%.2f', $stat );
          break;
        case $overall_config['event_3pt_per']:
        case $overall_config['event_fg_per']:
        case $overall_config['event_ft_per']:
          printf( '%.3f', $stat );
          break;
        default:
          printf( '%d', $stat );
          break;
    }
}

/**
 * calculate and print formatted career event stat
 *
 * @param int $eventId
 * @param int array $career values of the events stats
 */ 
function printCareerEventStat( $eventId, $career )
{
    global $overall_config;
    switch ($eventId)
    {
        case $overall_config['event_ppg']:
          if ($career['played']) {
            printf( '%.1f', $career[$overall_config['event_points']]/$career['played'] );
          }
          else echo '-';
          break;
        case $overall_config['event_rpg']:
          if ($career['played']) {
            printf( '%.1f', $career[$overall_config['event_rebounds']]/$career['played'] );
          }
          else echo '-';
          break;
        case $overall_config['event_apg']:
          if ($career['played']) {
            printf( '%.1f', $career[$overall_config['event_assists']]/$career['played'] );
          }
          else echo '-';
          break;
        case $overall_config['event_spg']:
          if ($career['played']) {
            printf( '%.2f', $career[$overall_config['event_steals']]/$career['played'] );
          }
          else echo '-';
          break;
        case $overall_config['event_bpg']:
          if ($career['played']) {
            printf( '%.2f', $career[$overall_config['event_blocks']]/$career['played'] );
          }
          else echo '-';
          break;
        case $overall_config['event_3pt_per']:
          if ($career[$overall_config['event_3pt_attempts']]) {
            printf( '%.3f', $career[$overall_config['event_3pt_made']]
                            /$career[$overall_config['event_3pt_attempts']]);
          }
          else echo '-';
          break;
        case $overall_config['event_fg_per']:
          if ($career[$overall_config['event_fg_attempts']]) {
            printf( '%.3f', $career[$overall_config['event_fg_made']]
                            /$career[$overall_config['event_fg_attempts']] );
          }
          else echo '-';
          break;
        case $overall_config['event_ft_per']:
          if ($career[$overall_config['event_ft_attempts']]) {
            printf( '%.3f', $career[$overall_config['event_ft_made']]
                            /$career[$overall_config['event_ft_attempts']] );
          }
          else echo '-';
          break;
        default:
          printf( '%d', $career[$eventId] );
          return;
    }    
}
?>

<!-- Header with Project Name-->
<?php if ($showPlayer_config['show_project_heading'] == "1" OR $overall_config['show_project_heading'] == "1") { ?>
<div class="componentheading" <?php if ($showPlayer_config['show_print_button'] == "1" OR $overall_config['show_print_button'] == "1") echo 'style="float:left;"';?>><?php echo HTML_joomleague::showTitle($joomleague, $overall_config['project_heading_type']); ?></div>
<!-- Druckfunktion -->
<?php if ($showPlayer_config['show_print_button'] == "1" OR $overall_config['show_print_button'] == "1") {
HTML_joomleague::PrintIcon( $row, $params, false, '' );
};?>
<!-- Druckfunktion Ende -->
<?php } ?>
<!-- Header END -->

<!-- Bereichsüberschrift z.B Tabelle, Tippabgabe usw. bei Bedarf mit Drucksymbol -->
<table width="100%" class="contentpaneopen">
	<tr>
		<td class="contentheading"><?php echo sprintf ('%s %s' ,$row->firstname,$row->lastname);?>
    	<?php if($row->injury > 0 && $row->injury_end > $joomleague->current_round){
        echo '<img src="'.JL_TEMPLATE_FOLDERS_URL.'/images/injured.gif" alt="" />';
              }?>
    	<?php if($row->suspension > 0 && $row->suspension_end > $joomleague->current_round){
        echo '<img src="'.JL_TEMPLATE_FOLDERS_URL.'/images/suspension.gif" alt="" />';
              }?>              
    </td>
	</tr>
</table>
<!-- Bereichsüberschrift Ende -->
<br />
  
<table width="96%" align="center" border="0" cellpadding="3" cellspacing="0">
	<tr class="sectiontableheader">
	 <td colspan="2"><?php echo _JL_PLAYER_DATA ;?></td>
	</tr>
	<tr>
  	<td align="center" valign="middle">
    <?php if ($showPlayer_config['show_player_photo']==1) {
      $picture = ($row->picture!=''&&!is_null($row->picture)&&file_exists(_JOOMLA_JL_PATH.'/'.$row->picture)) ? $row->picture : $row->default_picture;
      if ($picture!=''&&!is_null($picture)&&file_exists(_JOOMLA_JL_PATH.'/'.$picture)) {
        echo sprintf("<img src=\""._JOOMLA_JL_LIVE_SITE."/%s\" alt=\"%s\" title=\"%s\" />",$picture,$row->lastname,$row->lastname);
      } 
    }?>
    &nbsp;</td>   
      <td align="left" valign="top">
        <table width="100%" border="0">
          <tr>
            <td width="40%"><b><?php echo _JL_PLAYER_NAME;?></b></td>
            <td width="60%" align="left"><?php 
            $name = sprintf("%s %s",$row->firstname,$row->lastname);
            switch ( $showPlayer_config['show_user_profile'] ) {
            	case 1:
            		if ($contact_id) echo '<a href="'.sefRelToAbs("index.php?option=com_contact&task=view&contact_id=$contact_id").'">'.$name.'</a>';
            		else echo $name;
            		break;
            	case 2: 
            	  echo '<a href="'.sefRelToAbs("index.php?option=com_comprofiler&task=userProfile&user=$row->jl_user_id").'">'.$name.'</a>';
            		break;
            	default:
            		echo $name;
            		break;
            }
            ?>
            <?php
      $thiscountry = new Country($database);
      $thiscountry->load($row->nation);
      echo $thiscountry->getFlag();
     ?>
            
            </td>
          </tr>
<?php if($row->birthday != '0000-00-00'){ ?>
          <tr>
            <td><b><?php echo _JL_PLAYER_BIRTHDAY;?></b></td>
            <td align="left"><?php printf("%s (%s)", mbiFormatDate($row->birthday, _JL_DAYDATE), getAge($row->birthday)); ?></td>
          </tr>
<?php } ?>
<?php if($row->position != ""){ ?>
          <tr>
            <td><b><?php echo _JL_PLAYER_POSITION;?></b></td>
            <td align="left"><?php echo $row->position;?></td>
          </tr>
<?php } ?>
<?php if($row->position_number > 0){ ?>
          <tr>
            <td><b><?php echo _JL_PLAYER_NUMBER;?></b></td>
            <td align="left">
        <img src="<?php echo JL_TEMPLATE_FOLDERS_URL;?>/images/shirt.php?text=<?php echo ($row->position_number!=0) ? $row->position_number : '';?>" alt="<?php echo $row->position_number;?>" title="<?php echo $row->position_number;?>" />    
            </td>
          </tr>
<?php } ?>
<?php if($row->height > 0){ ?>
          <tr>
            <td><b><?php echo _JL_PLAYER_HEIGHT;?></b></td>
            <td align="left"><?php printf("%s cm",$row->height); ?></td>
          </tr>
<?php } ?>
<?php if($row->weight > 0){ ?>
          <tr>
            <td><b><?php echo _JL_PLAYER_WEIGHT;?></b></td>
            <td align="left"><?php printf("%s kg",$row->weight);?></td>
          </tr>
<tr><td>&nbsp;</td></tr>          
<?php } ?>
<?php if($row->injury > 0 && $row->injury_end > $joomleague->current_round){ ?>
          <tr>
            <td style="color: red"><b><?php echo _JL_PLAYER_INJURY_END;?></b></td>
            <td align="left"><?php printf("%s. %s",$row->injury_end, _JL_MATCHDAY); ?></td>
          </tr>
          <tr>
            <td nowrap="nowrap"><b><?php echo _JL_PLAYER_INJURY_TYPE;?></b></td>
            <td align="left"><?php printf("%s", htmlspecialchars($row->injury_detail)); ?></td>
          </tr>          
<?php } ?>
<?php if($row->suspension > 0 && $row->suspension_end > $joomleague->current_round){ ?>
          <tr>
            <td style="color: red"><b><?php echo _JL_PLAYER_SUSPENSION_END;?></b></td>
            <td align="left"><?php printf("%s. %s",$row->suspension_end, _JL_MATCHDAY); ?></td>
          </tr>
          <tr>
            <td nowrap="nowrap"><b><?php echo _JL_PLAYER_SUSPENSION_REASON;?></b></td>
            <td align="left"><?php printf("%s",htmlspecialchars($row->suspension_detail)); ?></td>
          </tr>
<?php } ?>
        </table>
      </td>
    </tr>

    </table>
  <br /><br />
<!-- Player history START -->  
  <?php 
  if ($showPlayer_config['show_player_history'] && count($history) > 0) { 
  ?>
  <table width="96%" align="center" border="0" cellpadding="3" cellspacing="0">
		<tr class="sectiontableheader">
			<td><?php echo _JL_PLAYER_HISTORY;?></td>
		</tr>
	  <tr>
		  <td>
			  <table id="player_history" width="100%" cellspacing="2" cellpadding="2" border="0">
			  <tr class="sectiontableheader">
			    <th><?php echo _JL_PLAYER_PROJECT;?></th>
			    <th><?php echo _JL_PLAYER_SEASON;?></th>
			    <th><?php echo _JL_TABLE_TEAM;?></th>
			    <th><?php echo _JL_PLAYER_POSITION;?></th>
			  </tr>
				<?php
				foreach ($history AS $station) {	?>
		    <tr><?php 
		        echo   '<td>'
                 . '<a href="'.sefRelToAbs(JL_COMPONENT_LINK.'&amp;func=showPlayer&amp;p='.$station->project_id.'&amp;pid='.$pid.JL_ITEMID_SUFFIX).'">'
                 . $station->pname
                 . '</a>'
                 . '</td>'
                 . '<td>'.$station->sname.'</td>'
		             . '<td>'
		             . '<a href="'.sefRelToAbs(JL_COMPONENT_LINK.'&amp;func=showPlayers&amp;p='.$station->project_id.'&amp;ttid='.$station->ttid.JL_ITEMID_SUFFIX).'">'
		             . $station->teamname
		             . '</a>'
		             . '</td>'
		             . '<td>'.$station->position.'</td>'; ?>
      	</tr><?php 
        } ?>
	      </table>
	    </td>
	  </tr>
  </table> 
<br /> 
<?php } ?>
<!-- Player history END --> 

<!-- Staff history START --> 
<?php if ($showPlayer_config['show_player_history'] && count($historyStaff) > 0) {;?>
<table width="96%" align="center" border="0" cellpadding="3" cellspacing="0">
	<tr class="sectiontableheader">
		<td><?php echo _JL_STAFF_HISTORY;?></td>
	</tr>
  <tr>
		<td>
		  <table id="staff_history" width="100%" cellspacing="2" cellpadding="2" border="0">
		   <tr class="sectiontableheader">
          <th><?php echo _JL_PLAYER_PROJECT;?></th>
          <th><?php echo _JL_PLAYER_SEASON;?></th>
          <th><?php echo _JL_TABLE_TEAM;?></th>
		      <th><?php echo _JL_PLAYERS_FUNCTION;?></td>
		  </tr><?php
      foreach ($historyStaff AS $station) {	?>
			<tr><?php
        echo  '<td>'
            . '<a href="'.sefRelToAbs(JL_COMPONENT_LINK.'&amp;func=showPlayer&amp;p='.$station->project_id.'&amp;pid='.$pid.JL_ITEMID_SUFFIX).'">'
            . $station->pname
            . '</a>'
            . '</td>'
            . '<td>'.$station->sname.'</td>'
            . '<td>'
            . '<a href="'.sefRelToAbs(JL_COMPONENT_LINK.'&amp;func=showPlayers&amp;p='.$station->project_id.'&amp;ttid='.$station->ttid.JL_ITEMID_SUFFIX).'">'
            . $station->teamname
            . '</a>'
            . '</td>'
            . '<td>'.$station->position.'</td>';?>
	   </tr><?php 
     } ?>
	   </table>
	 </td>
 </tr>
</table>
<br /><br />
<?php };?>
<!-- Staff history END --> 

<!-- Player description START --> 
<?php if ($showPlayer_config['show_player_description']==1 && $row->description !='') {;?>
<table width="96%" align="center" border="0" cellpadding="3" cellspacing="0">
	<tr class="sectiontableheader">
		<td><?php echo _JL_ADMIN_EDIT_FORM_PLAYERTOOL_TITLE2;?></td>
	</tr>
	<tr>
		<td>
			<?php echo $row->description;?>
		</td>
	</tr>
</table>
<br /><br />
<?php };?>
<!-- Player description END --> 


<!-- Player stats START --> 
<?php if ($showPlayer_config['show_player_statistic']==1) {;?>
<table width="96%" align="center" border="0" cellpadding="3" cellspacing="0">
	<tr class="sectiontableheader">
		<td><?php echo _JL_PLAYER_STATS;?></td>
	</tr>
	<tr>
    <td>
    <table id="stats_history" width="100%" cellspacing="2" cellpadding="2" border="0">
      <!-- header start -->
      <thead>
      <tr class="sectiontableheader">
          <th><?php echo _JL_PLAYER_PROJECT;?></th>
          <th><?php echo _JL_TABLE_TEAM;?></th>
          <th><?php echo _JL_PLAYER_PLAYED;?></th>
          <?php 
          if (isset($overall_config['use_jl_substitution']) && $overall_config['use_jl_substitution'] == 1) 
          {
            if ( $showPlayer_config['show_games_played']==1 )
            {
	            ?>
	          	<th><?php echo _JL_STARTING_ROSTERS;?></th>
	          	<?php
            }
            if ( $showPlayer_config['show_substitution_stats']==1 )
            {
              ?>
	            <th><img src="<?php echo JL_TEMPLATE_FOLDERS_URL.'/images/in.png';?>" alt="<?php echo _JL_PLAYER_IN;?>" />
	            </th>
	            <th><img src="<?php echo JL_TEMPLATE_FOLDERS_URL.'/images/out.png';?>" alt="<?php echo _JL_PLAYER_OUT;?>" />
	            </th>
              <?php
            }
          }
          if (count($eventtypes)>0) 
          {
            foreach($eventtypes as $eventtype) 
            {
              if (displayEvent($eventtype->eventtype_id))
              {
                ?>
                <th><?php echo $eventtype->name;?></th>
                <?php
              }
            }
          }
          ?>
      </tr>
      </thead>
      <!-- header end -->
      
<?php
    $career= array();
    foreach ($rows as $player_hist) {
    	if (!isset($career['played'])) $career['played'] = 0;
    	
      $inoutstat = BasketballProject::getStartInOutStats($pid, $player_hist->project_id, $player_hist->team_id);
      $career['played'] += $inoutstat->played;
      ?>
      <tr>
        <td align="left"><?php echo $player_hist->proj_name; ?></td>
        <td align="left"><?php echo $player_hist->team_name; ?></td>
        <td align="left"><?php echo ($inoutstat->played > 0) ? $inoutstat->played : '-'; ?></td>
        <?php
        //substitution system 
        if (isset($overall_config['use_jl_substitution']) && $overall_config['use_jl_substitution'] == 1) 
        {
    	    if (!isset($career['started'])) $career['started'] = 0;
          if (!isset($career['in'])) $career['in'] = 0;
          if (!isset($career['out'])) $career['out'] = 0;
          
          $career['started']+=$inoutstat->played - $inoutstat->subst;
	        $career['in']+=$inoutstat->subst;
	        $career['out']+=$inoutstat->sub_out;
	        if ( $showPlayer_config['show_games_played']==1 )
          {
            ?>
	          <td align="left">
	          <?php echo ($inoutstat->played - $inoutstat->subst > 0) ? $inoutstat->played - $inoutstat->subst : '-';?>
	          </td>
	          <?php
          }
	        if ( $showPlayer_config['show_substitution_stats']==1 )
          {
            ?>
		        <td align="left"><?php echo ($inoutstat->subst > 0) ? $inoutstat->subst : '-'; ?></td>
		        <td align="left"><?php echo ($inoutstat->sub_out > 0) ? $inoutstat->sub_out : '-'; ?></td>
		        <?php
          } 
				}

        // stats per project
        if (count($eventtypes)>0) 
        {
		      foreach($eventtypes as $eventtype) 
          {
	      	  $stat = BasketballProject::getPlayerEventStat($pid, $eventtype->eventtype_id, $player_hist->project_id, $player_hist->team_id);
	      	                
            // for career numbers calculation
            if (!isset($career[$eventtype->eventtype_id])) $career[$eventtype->eventtype_id] = 0;
            $career[$eventtype->eventtype_id]+=$stat;
            
	      	  if ( displayEvent($eventtype->eventtype_id) )
	      	  {
  		        ?>
  		        <td><?php printEventStat( $eventtype->eventtype_id, $stat );?></td>
              <?php 
		        }
		      }
	      } ?>
      </tr><?php
    } ?>
      <tr class="career_stats_total">
        <td align="left"><?php echo "Career"; ?></td>
        <td align="left">&nbsp;</td>
        <td align="left"><?php echo $career['played']; ?></td>
        <?php
        //substitution system
        if (isset($overall_config['use_jl_substitution']) && $overall_config['use_jl_substitution'] == 1) 
        {
          if ( $showPlayer_config['show_games_played']==1 )
          {
            ?>
            <td align="left"><?php echo $career['started'];?></td>
            <?php 
          }
          if ( $showPlayer_config['show_substitution_stats']==1 )
          {
            ?>
		        <td align="left"><?php echo $career['in']; ?></td>
		        <td align="left"><?php echo $career['out']; ?></td>
		        <?php
          }
        }

        // stats per project
        if (count($eventtypes)>0) 
        {
          foreach($eventtypes as $eventtype) 
          {
            if ( displayEvent($eventtype->eventtype_id) )
	      	  {
  		        ?>
  		        <td><?php printCareerEventStat( $eventtype->eventtype_id, $career );?></td>
              <?php 
		        }
          }
        } ?>
      </tr>
    </table>
    </td>
  </tr>
</table>
<?php };?>
<!-- Player stats END --> 
	
<!-- Print Button START -->
<?php if ($showPlayer_config['show_back_button'] > 0 OR $overall_config['show_back_button'] > 0) {;?>
<?php if ($showPlayer_config['show_back_button'] == "1" OR $overall_config['show_back_button'] == "1") echo '<div align="left">'; else echo '<div align="right">';?>
<?php
$params = & new mosParameters( '' );
$params->def( 'back_button', true );
$params->def( 'popup', false );
mosHTML::BackButton( $params );
?></div> 
<?php };?>
<!-- Print Button END -->  
